/* Created By Micha Sherman, google workshop 2012
 * 
 */

package com.sporteamws;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import android.view.View;
import android.widget.TextView;
import com.appengine.utils.AeLoadingCenter;
import com.appengine.utils.SavingAndLoadingCenter;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookUtils;
import com.facebook.android.Login;
import com.google.gwt.core.ext.linker.Shardable;
import com.gps.utilities.LocationBlank;

public class Intro extends Activity {
	/* global variables */
	public Facebook mFacebook = new Facebook(SharedPref.facebook_appID);
	String facebookID;
	TextView loadingInfo;
//	LocationManager lm = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);

		// set policies for android 4.03
		SetPolicies();

		// setup variables
		setupVariables();

		// check network connectivity

		if (isNetworkAvailable()) // online mode
		{

			SharedPref.GotNetworkConnection = true;
			loadingInfo.setText("loading initial information..please wait");

			// execute main backround loading
			AeLoadingCenter.LoadMainFromIntro(mFacebook, Intro.this,
					loadingInfo);

		} else // offline mode
		{
			SharedPref.GotNetworkConnection = false;
			loadingInfo.setText("no internet connection");
			boolean gotPrevSession = handleNoConnectionState();
			if (gotPrevSession)
				AeLoadingCenter.LoadMainFromIntro(mFacebook, Intro.this,
						loadingInfo);
		}
	//	lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	//	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, SharedPref.lb);
	}

	public void setupVariables() {
		loadingInfo = (TextView) findViewById(R.id.tvLoadingInfo);
		loadingInfo.setTextColor(0xFF000000);
	}

	private boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();

		if (activeNetworkInfo == null)
			return false;

		NetworkInfo i = connectivityManager.getActiveNetworkInfo();
		if (i == null)
			return false;
		if (!i.isConnected() || !i.isAvailable())
			return false;

		return true;

	}

	private boolean handleNoConnectionState() {
		String loadedFacebookID = SavingAndLoadingCenter.loadFB_ID(Intro.this);

		if (loadedFacebookID != null)
			return true;
		else {
			findViewById(R.id.loadingPB).setVisibility(View.INVISIBLE);
			showAlertDialog("you must login with facebook first to use SporTeam, please connect to the internet");
			return false;
		}
	}

	@SuppressWarnings("deprecation")
	private void showAlertDialog(String msg) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		// alertDialog.setTitle("Houston, We've Got a Problem");
		alertDialog.setMessage(msg);
		alertDialog.setIcon(R.drawable.alert);

		alertDialog.setButton("exit", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});

		alertDialog.show();
	}

	@Override
	protected void onPause() {
		// release resources
		super.onPause();
	}

	@Override
	protected void onStop() {
		// release resources
		super.onStop();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.finish();
		finish();
	}

	// set policies for android 4 and up (for use of network IO from main UI
	// thread)
	public void SetPolicies() {
		/*
		 * StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		 * .detectDiskReads() .detectDiskWrites() .detectNetwork() // or
		 * .detectAll() for all detectable problems .penaltyLog() .build());
		 * 
		 * StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		 * .detectLeakedSqlLiteObjects() .detectLeakedSqlLiteObjects()
		 * .penaltyLog() .penaltyDeath() .build());
		 */
	}

}
